package com.echo.tagparse;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.echo.tagparse.face.StemFace;

import edu.stanford.nlp.tagger.maxent.MaxentTagger;

public class ScoreMachine {
	static String tempFile = null;
	StringBuffer posSeeds = new StringBuffer();
	StringBuffer negSeeds = new StringBuffer();
	static StringBuffer features = new StringBuffer();
	BufferedReader reader = null;
	static Map<String, Integer> finalMap = new HashMap<String, Integer>(); 
	
	public ScoreMachine()throws Exception{
		tempFile = this.getClass().getResource("/").getPath()+"\\com\\echo\\res\\DigitalCamera.txt";
		String posFile = this.getClass().getResource("/").getPath()+"\\com\\echo\\res\\positiveSeeds.txt";
		String negFile = this.getClass().getResource("/").getPath()+"\\com\\echo\\res\\negativeSeeds.txt";
		String featureFile = this.getClass().getResource("/").getPath()+"\\com\\echo\\res\\feature.txt";
		String line = new String();
		
//		StemFace sf = new StemFace();
//		sf.StemStr(tempFile);
//		reader = new BufferedReader(new InputStreamReader(new FileInputStream(tempFile)));
		
//		while((line=reader.readLine())!=null){
//			line.trim();
//			System.out.println("");
//			searchNeighbor(line);
//		}
//		reader.close();
		
		reader = new BufferedReader(new InputStreamReader(new FileInputStream(posFile)));
		while((line=reader.readLine())!=null){
			posSeeds.append(line);
		}
		reader.close();
		
		reader = new BufferedReader(new InputStreamReader(new FileInputStream(negFile)));
		while((line=reader.readLine())!=null){
			negSeeds.append(line);
		}
		reader.close();
		
		reader = new BufferedReader(new InputStreamReader(new FileInputStream(featureFile)));
		while((line=reader.readLine())!=null){
			features.append(line);
		}
		reader.close();
		
	}
	
	public void searchNeighbor(String sentence)throws Exception{
		String POSModel = this.getClass().getResource("/").getPath()+"\\com\\echo\\nlp\\models\\left3words-wsj-0-18.tagger";
		MaxentTagger tagger = new MaxentTagger(POSModel);
	    String POSStr = MaxentTagger.tagString(sentence);
	    Pattern expression = Pattern.compile("[a-zA-Z]+/NN");
	    Matcher matcher = expression.matcher(POSStr);
	    Object word = null;
	    while(matcher.find()){
	    	word = matcher.group();
	    	System.out.print(word+" ");
	    }
//	    reader.close();
	}
	
	public int scoreReview(String reviewStr)throws Exception{
		int score = 0;
		String[] posTerms = posSeeds.toString().split(" ");
		String[] negTerms = negSeeds.toString().split(" ");
		String line = new String();
		
		reader = new BufferedReader(new InputStreamReader(new FileInputStream(reviewStr)));
		while((line=reader.readLine())!=null){
			String[] temp = line.split(" ");
			for(int m=0; m<temp.length; m++){
				for(int i=0; i<posTerms.length; i++){
					if(temp[m].equals((String)posTerms[i])){
						score++;
						System.out.print(temp[m]+" ");
						System.out.println(score);
					}
				}
				for(int j=0; j<negTerms.length; j++){
					if(temp[m].equals((String)negTerms[j])){
						score--;
						System.out.print(temp[m]+" ");
						System.out.println(score);
					}
				}
			}
		}
		
		return score ;
	}
	
	public int scoreSentence(String sentence){
		int score = 0;
		String[] posTerms = posSeeds.toString().split(" ");
		String[] negTerms = negSeeds.toString().split(" ");
//		String posTerms = posSeeds.toString();
//		String negTerms = negSeeds.toString();
		boolean noFlag = false;
		String[] temp = sentence.split(" ");
		for(int m=0; m<temp.length; m++){
			if (temp[m].equals("not")||temp[m].equals("no")||temp[m].equals("n't")){
				noFlag = true;
				System.out.print(temp[m]+" ");
			}
			for(int i=0; i<posTerms.length; i++){
				if(posTerms[i].equals(temp[m])){
					if(noFlag){
						score=score-2;
						System.out.print(temp[m]+" ");
						System.out.println(score);
					}else{
						score=score+1;
						System.out.print(temp[m]+" ");
						System.out.println(score);
					}
					break;
				}
			}
			for(int j=0; j<negTerms.length; j++){
				if(negTerms[j].equals(temp[m])){
					if(noFlag){
						score=score+1;
						System.out.print(temp[m]+" ");
						System.out.println(score);
					}else{
						score=score-2;
						System.out.print(temp[m]+" ");
						System.out.println(score);
					}
					break;
				}	
			}
		}
		return score;
	}
	
	public HashMap<String, Integer> scoreFullReview(String fullReview, String[] featureTerms, HashMap<String, Integer> tempMap)throws Exception{
//		int finalScore = 0;
		HashMap<String, Integer> subMap = tempMap;
//		String[] featureTerms = features.toString().split(" ");
		String[] sentences = fullReview.split("[,.!?]");
		
		for(int i=0; i<sentences.length; i++){
			for(int m=0; m<featureTerms.length; m++){
				if(sentences[i].contains(featureTerms[m])){
					
//					finalScore += scoreSentence(sentences[i]);
					int sentenceScore = scoreSentence(sentences[i]);
					int newScore = subMap.get(featureTerms[m])+sentenceScore;
					subMap.put(featureTerms[m], newScore);
					break;
				}
//				System.out.print(featureTerms[m]+" "+ finalScore);
			}
		}
//		Iterator iter = subMap.entrySet().iterator();
//		while(iter.hasNext()){
//			Entry entry = (Entry)iter.next();
//			Object key = entry.getKey();
//			Object val = entry.getValue();
//			System.out.print(key.toString()+": "+val.toString()+"  ");
//		}
//		System.out.println('\n'+"****************");
		return subMap;
	}
	
	
	public static void main(String[] args){
		try{
//			HashMap al = new HashMap();
//			FileInputStream fileStream = new FileInputStream("c:\\weighttable.ser");
//			BufferedInputStream br = new BufferedInputStream(fileStream);
//			ObjectInputStream in = new ObjectInputStream(br);
//			al = (HashMap)in.readObject();
//			in.close();
			//stemmer
//			StemFace sf = new StemFace();
//			sf.StemStr("c://negativeSeeds.txt");
			ScoreMachine sm = new ScoreMachine();
			String[] featureTerms = features.toString().split(" ");
			for(int m=0; m<featureTerms.length; m++){
				String key = featureTerms[m];
				finalMap.put(key, 0);
			}
			String line = new String();
			BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(tempFile)));
			FileWriter fw = new FileWriter("c:\\camera.csv");
			boolean titleFlag = true;
			
			//get a full review 
			while((line=reader.readLine())!=null){
				int finalScore = 0 ;
				HashMap<String, Integer> subMap = new HashMap<String, Integer>(); 
				for(int m=0; m<featureTerms.length; m++){
					String key = featureTerms[m];
					subMap.put(key, 0);
				}
				if(titleFlag){
					Iterator iter = subMap.entrySet().iterator();
					while(iter.hasNext()){
						Entry entry = (Entry)iter.next();
						Object key = entry.getKey();
						fw.write(key.toString()+",");
					}
//					fw.write("Score,Rating\r\n");
					fw.write("Rating\r\n");
					titleFlag = false;
				}
				String[] reviews = line.split("[|]");
				String[] temp = (reviews[2]+reviews[3]+reviews[4]).split("[.]");
				for(int i=0; i<temp.length; i++){
					subMap = sm.scoreFullReview(temp[i], featureTerms, subMap);
					
//				finalMap = sm.scoreFullReview(reviews[3], featureTerms, subMap);
				}
//				sm.scoreFullReview(reviews[1]+reviews[2]+reviews[3], featureTerms, subMap);
				Iterator iter = subMap.entrySet().iterator();
				int reviewScore = 0;
				while(iter.hasNext()){
					Entry entry = (Entry)iter.next();
					Object key = entry.getKey();
					Object val = entry.getValue();
					System.out.print(key.toString()+": "+val.toString()+"  ");
					int tempScore = Integer.parseInt(val.toString());
					
//					if(tempScore>=2){
////						fw.write("positive,");
//						tempScore = tempScore*50;
//					}else if(tempScore<0){
////						fw.write("negative,");
//						tempScore = tempScore*100;
//					}else if((tempScore==1)||(tempScore==0)){
////						fw.write("neutral,");
//						tempScore = tempScore*20;
//					}
//					reviewScore = reviewScore+tempScore;
					if(key.equals("quality")){
						tempScore = tempScore*161;
					}else if(key.equals("pictures")){
						tempScore = tempScore*120;
					}else if(key.equals("battery")){
						tempScore = tempScore*100;
					}else if(key.equals("zoom")){
						tempScore = tempScore*72;
					}else if(key.equals("price")){
						tempScore = tempScore*66;
					}else if(key.equals("use")){
						tempScore = tempScore*62;
					}else if(key.equals("life")||key.equals("picture")){
						tempScore = tempScore*54;
					}else if(key.equals("features")){
						tempScore = tempScore*48;
					}
					fw.write(tempScore+",");
				}
//				fw.write(reviewScore+",");
				if(reviews[1].equals("5")||reviews[1].equals("4")){
					fw.write("Positive");
				}else if(reviews[1].equals("3")){
					fw.write("Neutral");
				}else if(reviews[1].equals("2")||reviews[1].equals("1")){
					fw.write("Negative");
				}
				fw.write("\r\n");
				System.out.println('\n'+"****************");
			}
			fw.close();
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
